﻿using OnlineFIR.Utilities;
using OnlineFIR.Models;

namespace OnlineFIR.Mechanic
{
    public class WinCheck
    {
        private static Map mymap;
        public static bool Check(Map map,int x,int y)
        {
            return Check(map, new Point(x, y));
        }
        public static bool Check(Map map,Point p)
        {
            mymap = map.Copy();
            byte me = mymap.GetOne(p);
            byte turnInto = (byte)(me + 2);
            List <Point> res = new List<Point>();
            for(int i = 0; i < 4; i++)
            {
                res = Extend(p, i, me);
                if (res.Count >= 5)
                    break;
            }
            if (res.Count >= 5)
            {
                foreach (Point item in res)
                {
                    map.Set(item,turnInto);
                }
                return true;
            }
            return false;
        }
        private static List<Point> Extend(Point p, int dir, byte me)
        {
            List<Point> res = new List<Point>();
            res.Add(p);
            //dir=0表示上下，1表示右上左下，2表示右左，3表示右下左上
            if (dir == 0)
            {
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x - i, p.y);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x + i, p.y);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
            }
            else if (dir == 1)
            {
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x - i, p.y + i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x + i, p.y - i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
            }
            else if (dir == 2)
            {
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x, p.y + i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x, p.y - i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
            }
            else if (dir == 3)
            {
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x + i, p.y + i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
                for (int i = 1; i < 6; i++)
                {
                    Point testing = new Point(p.x - i, p.y - i);
                    if (mymap.IsInside(testing) && (mymap.GetOne(testing) == me))
                        res.Add(testing);
                    else
                        break;
                }
            }

            return res;
        }
    }
}
